package com.taptech.doufu.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taptech.doufu.util.ScreenUtil2;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.bean.HomeTopBean;
import com.taptech.doufu.bean.ImageBean;
import com.taptech.doufu.bean.NovelBean;
import com.taptech.doufu.bean.UserBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.ui.view.personalcenter.ContentTagsViewGroup;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.ImageManager;
import com.taptech.doufu.util.StartActivityUtil;
import com.taptech.doufu.util.UIUtil;

import java.util.List;

public class TTSearchContentAdapter extends BaseListAdapter
{
	Context mContext;

	/** 视频 */
	private final int VIDEO = 0;
	/** 小说 */
	private final int NOVEL = 1;
	/** 话题 */
	private final int TOPIC = 2;
	/** 文评 */
	private final int SEARCH_SWEEP = 3;
	/** 小说摘要 */
	private final int SEARCH_DIGEST = 4;

	private String currentKey = "";
	private List<HomeTopBean> mainList;
	static int width, space, rightSpace;
	private int leftMargin, rightMarin;

	public TTSearchContentAdapter(Context context)
	{
		// TODO Auto-generated constructor stub
		mContext = context;
		if (width == 0)
		{
			space = ScreenUtil2.dip2px(11);
			rightSpace = ScreenUtil2.dip2px(11);

			int macthWidth = ScreenUtil2.SCREEN_PX_WIDTH - space * 3 - 2 * rightSpace;
			width = macthWidth / 3;
		}
		leftMargin = (int) context.getResources().getDimension(R.dimen.home_space_left);
		rightMarin = (int) context.getResources().getDimension(R.dimen.home_space_right);

	}

	@Override
	public int getItemViewType(int position)
	{
		int type = 0;
		HomeTopBean bean = (HomeTopBean) getDataSource().get(position);
		int object_type = DiaobaoUtil.String2Int(bean.getObject_type());
		if (object_type == Constant.CONTENT_TYPE_TOPIC)
		{
			int topic_type = DiaobaoUtil.String2Int(bean.getTopic_type());
			type = objectType(topic_type);
		} else
		{
			type = objectType(object_type);
		}
		return type;
	}

	@Override
	public int getViewTypeCount()
	{
		// TODO Auto-generated method stub
		return 5;
	}

	@SuppressWarnings("null")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent)
	{
		NovelHolder novelHolder = null;
		VideoHolder videoHolder = null;
		TopicHolder topicHolder = null;
		SweepHolder sweepHolder = null;
		DigestHolder digestHolder = null;
		int type = getItemViewType(position);
		if (convertView == null)
		{
			switch (type)
			{
			case VIDEO:
				videoHolder = new VideoHolder();
				convertView =  LayoutInflater.from(parent.getContext()).inflate(R.layout.search_content_video_item, null);
				videoHolder.videoTitle = (TextView) convertView.findViewById(R.id.search_content_video_item_title);
				videoHolder.videoCount = (TextView) convertView.findViewById(R.id.search_content_video_item_count);
				videoHolder.commentCount = (TextView) convertView.findViewById(R.id.search_content_video_item_comment_count);
				videoHolder.videoLog = (ImageView) convertView.findViewById(R.id.search_content_video_item_log);
				videoHolder.videoTag = (ImageView) convertView.findViewById(R.id.search_content_video_item_tag);
				videoHolder.videoTag.setVisibility(View.VISIBLE);
				convertView.setTag(videoHolder);
				break;
			case NOVEL:
				novelHolder = new NovelHolder();
				convertView =  LayoutInflater.from(parent.getContext()).inflate(R.layout.search_content_novel_item, null);
				novelHolder.novelTitle = (TextView) convertView.findViewById(R.id.search_content_novel_item_title);
				novelHolder.novelAuthor = (TextView) convertView.findViewById(R.id.search_content_novel_item_author);
				novelHolder.novelDes = (TextView) convertView.findViewById(R.id.search_content_novel_item_des);
				novelHolder.novelTag = (ImageView) convertView.findViewById(R.id.search_content_novel_item_tag);
				convertView.setTag(novelHolder);
				break;
			case TOPIC:
				topicHolder = new TopicHolder();
				convertView =  LayoutInflater.from(parent.getContext()).inflate(R.layout.search_content_topic_item, null);
				topicHolder.topicTitle = (TextView) convertView.findViewById(R.id.search_content_topic_item_title);
				topicHolder.topicAuthor = (TextView) convertView.findViewById(R.id.search_content_topic_item_author);
				topicHolder.topicDes = (TextView) convertView.findViewById(R.id.search_content_topic_item_des);
				topicHolder.contentImage1 = (ImageView) convertView.findViewById(R.id.search_content_topic_item_imgge1);
				topicHolder.contentImage2 = (ImageView) convertView.findViewById(R.id.search_content_topic_item_imgge2);
				topicHolder.contentImage3 = (ImageView) convertView.findViewById(R.id.search_content_topic_item_imgge3);
				topicHolder.topicTag = (ImageView) convertView.findViewById(R.id.search_content_topic_item_tag);
				LinearLayout.LayoutParams im_lp = new LinearLayout.LayoutParams(-1, width, 1);
				im_lp.setMargins(space, 0, 0, 0);
				topicHolder.contentImage1.setLayoutParams(im_lp);
				topicHolder.contentImage2.setLayoutParams(im_lp);
				topicHolder.contentImage3.setLayoutParams(im_lp);
				topicHolder.imagegropLaoyout = (LinearLayout) convertView.findViewById(R.id.search_content_topic_item_image_group);
				convertView.setTag(topicHolder);
				break;
			case SEARCH_SWEEP:
				convertView =  LayoutInflater.from(parent.getContext()).inflate(R.layout.home_item_sweep_new, parent, false);
				sweepHolder = new SweepHolder();
				sweepHolder.sweepTitle = (TextView) convertView.findViewById(R.id.home_item_sweep_title);
				sweepHolder.sweepDes = (TextView) convertView.findViewById(R.id.home_item_sweep_des);

				sweepHolder.sweepTaglist = (ContentTagsViewGroup) convertView.findViewById(R.id.home_item_sweep_taglist);
				sweepHolder.sweepReadTimeImg = (ImageView) convertView.findViewById(R.id.home_item_sweep_read_time_icon);
				sweepHolder.sweepReadTimes = (TextView) convertView.findViewById(R.id.home_item_sweep_read_times);
				sweepHolder.flowerCount = (TextView) convertView.findViewById(R.id.home_item_sweep_flower_count);
				sweepHolder.commentCount = (TextView) convertView.findViewById(R.id.home_item_comment_count);
				sweepHolder.mUserImg = (ImageView) convertView.findViewById(R.id.home_item_sweep_userimg);
				sweepHolder.mUserName = (TextView) convertView.findViewById(R.id.home_item_sweep_username);
				sweepHolder.mAuthorName = (TextView) convertView.findViewById(R.id.home_item_sweep_author);
				sweepHolder.mTitleLevel = (TextView) convertView.findViewById(R.id.home_item_sweep_title_level);
				sweepHolder.mCreateTime = (TextView) convertView.findViewById(R.id.home_item_sweep_create_time);
				/*sweepHolder.sweepTag = (ImageView) convertView.findViewById(R.id.home_item_sweep_tag);
				sweepHolder.sweepTag.setVisibility(View.VISIBLE);*/
				convertView.setTag(sweepHolder);
				break;
			case SEARCH_DIGEST:
				convertView =  LayoutInflater.from(parent.getContext()).inflate(R.layout.search_content_item_digest, parent, false);
				digestHolder = new DigestHolder();
				digestHolder.mTitle = (TextView) convertView.findViewById(R.id.search_content_item_digest_title);
				digestHolder.mAuthorName = (TextView) convertView.findViewById(R.id.search_content_item_digest_author);
				digestHolder.des = (TextView) convertView.findViewById(R.id.search_content_item_digest_des);
				convertView.setTag(digestHolder);
				break;
			default:
				break;
			}

		} else
		{
			switch (type)
			{
			case VIDEO:
				videoHolder = (VideoHolder) convertView.getTag();
				break;
			case NOVEL:
				novelHolder = (NovelHolder) convertView.getTag();
				break;
			case TOPIC:
				topicHolder = (TopicHolder) convertView.getTag();
				break;
			case SEARCH_SWEEP:
				sweepHolder = (SweepHolder) convertView.getTag();
				break;
			case SEARCH_DIGEST:
				digestHolder = (DigestHolder) convertView.getTag();
				break;
			default:
				break;
			}
		}
		final HomeTopBean bean = (HomeTopBean) getDataSource().get(position);
		switch (type)
		{
		case VIDEO:
			videoHolder.videoTitle.setText(DiaobaoUtil.textLighterColor(bean.getTitle(), this.currentKey));
			// videoHolder.videoCount.setText(DiaobaoUtil.textLighterColor(bean.getDes(),
			// this.currentKey));
			videoHolder.videoCount.setText("一共" + bean.getArticle_num() + "集");
			videoHolder.commentCount.setText(bean.getComment_times());
			if (bean.getImages() != null && bean.getImages().length > 0)
			{
				ImageManager.displayImage(videoHolder.videoLog, bean.getImages()[0].getImgUrl(), 0);
			} else
			{
				videoHolder.videoLog.setImageResource(R.drawable.bg_home_photo_default);
			}
			break;
		case NOVEL:
			novelHolder.novelTitle.setText(DiaobaoUtil.textLighterColor(bean.getTitle(), this.currentKey));
			novelHolder.novelAuthor.setText(DiaobaoUtil.textLighterColor(bean.getAuthor(), this.currentKey));
			novelHolder.novelDes.setText(DiaobaoUtil.textLighterColor(bean.getDes(), this.currentKey));
			break;
		case TOPIC:
			topicHolder.topicTitle.setText(DiaobaoUtil.textLighterColor(bean.getTitle(), this.currentKey));
			topicHolder.topicAuthor.setText(DiaobaoUtil.textLighterColor(bean.getUser().getName(), this.currentKey));
			String des = bean.getDes();
			if ("".equals(des))
			{
				topicHolder.topicDes.setVisibility(View.GONE);
			} else
			{
				topicHolder.topicDes.setVisibility(View.VISIBLE);
				topicHolder.topicDes.setText(DiaobaoUtil.textLighterColor(bean.getDes(), this.currentKey));
			}

			ImageBean[] imageBean = bean.getImages();
			if (imageBean.length > 0)
			{
				topicHolder.imagegropLaoyout.setVisibility(View.VISIBLE);
				String imageUrl = imageBean[0].getImgUrl();
				String imageUrl2 = null;
				String imageUrl3 = null;
				if (imageBean.length == 1)
				{
					imageUrl = imageBean[0].getImgUrl();
					topicHolder.contentImage2.setVisibility(View.INVISIBLE);
					topicHolder.contentImage3.setVisibility(View.INVISIBLE);
				} else if (imageBean.length == 2)
				{
					imageUrl2 = imageBean[1].getImgUrl();
					topicHolder.contentImage2.setVisibility(View.VISIBLE);
					topicHolder.contentImage3.setVisibility(View.INVISIBLE);
				} else
				{
					imageUrl2 = imageBean[1].getImgUrl();
					imageUrl3 = imageBean[2].getImgUrl();
					topicHolder.contentImage2.setVisibility(View.VISIBLE);
					topicHolder.contentImage3.setVisibility(View.VISIBLE);
				}

				topicHolder.contentImage1.setImageResource(R.drawable.bg_home_photo_default);
				topicHolder.contentImage2.setImageResource(R.drawable.bg_home_photo_default);
				topicHolder.contentImage3.setImageResource(R.drawable.bg_home_photo_default);
				if (imageUrl != null)
				{
					ImageManager.displayImage(topicHolder.contentImage1, imageUrl, 0);

				}
				if (imageUrl2 != null)
				{
					ImageManager.displayImage(topicHolder.contentImage2, imageUrl2, 0);

				}
				if (imageUrl3 != null)
				{
					ImageManager.displayImage(topicHolder.contentImage3, imageUrl3, 0);

				}
			} else
			{
				topicHolder.imagegropLaoyout.setVisibility(View.GONE);
			}
			break;
		case SEARCH_SWEEP:
			//novelHolder.novelDes.setText(DiaobaoUtil.textLighterColor(bean.getComment_times(), this.currentKey));
			
			final UserBean userBean = bean.getUser();
			// SweepBean sweepBean = bean.getSweep();
			final NovelBean novelBean = bean.getNovel();
			if (novelBean.getTitle() != null){
				sweepHolder.sweepTitle.setText(DiaobaoUtil.textLighterColor(novelBean.getTitle(), this.currentKey));
			}
			sweepHolder.mAuthorName.setText(DiaobaoUtil.textLighterColor("作者：" + novelBean.getAuthor(), this.currentKey));
			sweepHolder.flowerCount.setText(bean.getFlower_num());
			sweepHolder.commentCount.setText(bean.getComment_times());
			sweepHolder.mAuthorName.setVisibility(View.VISIBLE);
			if (bean.getReview() != null)
			{
				// TextUtil.handleTextView(sweepHolder.sweepDes,
				// sweepBean.getReview(),5);
				sweepHolder.sweepDes.setText(DiaobaoUtil.textLighterColor(bean.getReview(), this.currentKey));
			}
			final String novelId = novelBean.getId();
			convertView.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					SimpleWeexActivity.startActivity(v.getContext(),"vue/articalComment/TFArticalComment.js?novelId="+novelBean.getId());
				}
			});
			sweepHolder.mUserImg.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					StartActivityUtil.startUserActivity(userBean, v.getContext());
				}
			});
			// 文评信息
			GlideUtil.displayCircleImage(sweepHolder.mUserImg, userBean.getIcon());
			sweepHolder.mUserName.setText(DiaobaoUtil.textLighterColor(userBean.getName(), this.currentKey));

			// 标签信息
			if (bean.getTag_list() != null) {

				String[] tagStrings = bean.getTag_list().split("\\s+");
				sweepHolder.sweepTaglist.setTextTags(mContext , tagStrings);
			}

			if (bean.getStar() != null)
			{
				UIUtil.showStarLevel(Integer.valueOf(bean.getStar()), sweepHolder.mTitleLevel);
			}
			// 浏览次数
			if (bean.getRead_num() != null)
			{
				if (bean.getRead_num().equals("0"))
				{
					sweepHolder.sweepReadTimes.setVisibility(View.GONE);
					sweepHolder.sweepReadTimeImg.setVisibility(View.GONE);
				} else
				{
					sweepHolder.sweepReadTimes.setVisibility(View.VISIBLE);
					sweepHolder.sweepReadTimeImg.setVisibility(View.VISIBLE);
					sweepHolder.sweepReadTimes.setText(bean.getRead_num());
				}
			}
			if (bean != null && bean.getAdd_time() != null) {
				sweepHolder.mCreateTime.setText(DiaobaoUtil.seconds2TimeString(Long.parseLong(bean.getAdd_time())));
			}
			break;
		case SEARCH_DIGEST:
			//DiaobaoUtil.textLighterColor("作者：" + bean.getAuthor(), this.currentKey)
			digestHolder.mTitle.setText(DiaobaoUtil.textLighterColor(bean.getTitle(), this.currentKey));
			digestHolder.mAuthorName.setText(DiaobaoUtil.textLighterColor("作者：" + bean.getAuthor(), this.currentKey));
			//digestHolder.des.setText(bean.getContent());
			digestHolder.des.setText(DiaobaoUtil.textLighterColor(bean.getContent(), this.currentKey));
			convertView.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					SimpleWeexActivity.startActivity(v.getContext(),"vue/articalComment/TFArticalComment.js?novelId="+bean.getId());

				}
			});
			break;
		default:
			break;
		}
		return convertView;
	}

	public static class NovelHolder
	{
		TextView novelTitle;
		TextView novelAuthor;
		TextView novelDes;
		ImageView novelTag;
	}

	public static class VideoHolder
	{
		TextView videoTitle;
		TextView videoCount;
		TextView commentCount;
		ImageView videoLog;
		ImageView videoTag;
	}

	public static class TopicHolder
	{
		TextView topicTitle;
		TextView topicAuthor;
		TextView topicDes;
		ImageView contentImage1, contentImage2, contentImage3, topicTag;
		LinearLayout imagegropLaoyout;
	}

	public class SweepHolder
	{
		TextView mTitleLevel;
		TextView sweepTitle;
		TextView sweepDes;
		ContentTagsViewGroup sweepTaglist;
		TextView sweepReadTimes;
		TextView mUserName;
		TextView mAuthorName;
		TextView flowerCount;
		TextView commentCount;
		ImageView sweepReadTimeImg, sweepTag;
		ImageView mUserImg;
		TextView mCreateTime;

	}

	public class DigestHolder
	{
		TextView mTitle;
		TextView des;
		TextView mAuthorName;
	}

	public HomeTopBean getBean(int postion)
	{
		HomeTopBean topBean = null;
		if (postion >= 0 && getDataSource() != null && postion < getDataSource().size())
		{
			topBean = (HomeTopBean) getDataSource().get(postion);
		}
		return topBean;

	}

	private int objectType(int type)
	{
		int contentType = 0;
		switch (type)
		{
		case Constant.OBJECT_TYPE_NOVEL:
			contentType = NOVEL;
			break;
		case Constant.OBJECT_TYPE_USERS_TOPIC:
			contentType = TOPIC;
			break;
		case Constant.OBJECT_TYPE_SWEEP:
			contentType = SEARCH_SWEEP;
			break;
		case Constant.OBJECT_TYPE_DIGEST:
			contentType = SEARCH_DIGEST;
			break;
		default:
			break;
		}
		return contentType;
	}

	public void setSearchContent(String currentKey)
	{
		this.currentKey = currentKey;
	}
}
